<template>
	<view class="page">
		<uni-forms :modelValue="formData" label-width="200rpx">
			<uni-forms-item label="扫描箱体码" name="shebei">
				<uni-easyinput type="text" v-model="formData.shebei" placeholder="请输入箱体码(标签打印)" suffixIcon="scan"
					@iconClick="iconClick1"/>
			</uni-forms-item>
			<uni-forms-item label="扫描屏幕码" name="zhongduan">
				<uni-easyinput type="text" v-model="formData.zhongduan" placeholder="请输入屏幕码(屏幕显示)" suffixIcon="scan"
					@iconClick="iconClick2"/>
			</uni-forms-item>
		</uni-forms>
		<u-button class="btn" @click="submit" type="primary" text="提交" />
	</view>
</template>

<script>
	import {
		setTerminalSN
	} from '@/api/common.js';
	export default {
		data() {
			return {
				formData: {
					shebei:'',
					zhongduan:''
				},
			}
		},
		methods: {
			iconClick1(type) {
				console.log('點擊了掃碼。。。')
				// 允许从相机和相册扫码
				var that = this
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.formData.shebei = res.result
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '此设备暂不支持扫码功能',
						})
					}
				});
			},
			iconClick2(type) {
				console.log('點擊了掃碼。。。')
				// 允许从相机和相册扫码
				var that = this
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.formData.zhongduan = res.result
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '此设备暂不支持扫码功能',
						})
					}
				});
			},
			submit() {
				setTerminalSN({
					syscode: this.formData.shebei,
					terminalcode: this.formData.zhongduan
				}).then(res => {
					console.log('查詢結果：' + JSON.stringify(res));
					uni.showToast({
						title: "修改成功",
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 500)
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		margin-top: 32rpx;
		width: 710rpx;
		margin-left: 16rpx;
		margin-right: 16rpx;
	}
</style>